package com.zenithmind.chat.controller;

import com.zenithmind.chat.pojo.vo.ChatMessageReadVO;
import com.zenithmind.chat.service.ChatMessageReadService;
import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 消息已读状态控制器 - 遵循单一职责原则
 * 只负责消息已读状态的HTTP接口处理
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Slf4j
@RestController
@RequestMapping("/api/chat/message-reads")
@RequiredArgsConstructor
@Validated
@Tag(name = "消息已读管理", description = "消息已读状态相关接口")
public class ChatMessageReadController {

    private final ChatMessageReadService chatMessageReadService;

    @PostMapping("/mark-read")
    @Operation(summary = "标记消息已读", description = "标记指定消息为已读状态")
    public Result<Boolean> markMessageAsRead(
            @Parameter(description = "消息ID") @RequestParam @NotBlank String messageId) {
        String userId = UserContext.getUserId();
        Boolean result = chatMessageReadService.markAsRead(messageId, userId);
        return Result.success(result);
    }

    @PostMapping("/batch-mark-read")
    @Operation(summary = "批量标记消息已读", description = "批量标记多个消息为已读状态")
    public Result<Boolean> batchMarkMessagesAsRead(@RequestBody @NotEmpty List<String> messageIds) {
        String userId = UserContext.getUserId();
        Boolean result = chatMessageReadService.batchMarkAsRead(messageIds, userId);
        return Result.success(result);
    }

    @PostMapping("/mark-all-read")
    @Operation(summary = "标记全部已读", description = "标记群组或会话中的所有消息为已读")
    public Result<Boolean> markAllAsRead(
            @Parameter(description = "群组ID") @RequestParam(required = false) String groupId,
            @Parameter(description = "会话ID") @RequestParam(required = false) String sessionId) {
        String userId = UserContext.getUserId();
        Boolean result = chatMessageReadService.markAllAsRead(groupId, sessionId, userId);
        return Result.success(result);
    }

    @GetMapping("/message/{messageId}/readers")
    @Operation(summary = "查询消息已读用户", description = "查询指定消息的已读用户列表")
    public Result<List<ChatMessageReadVO>> getMessageReaders(
            @Parameter(description = "消息ID") @PathVariable @NotBlank String messageId) {
        List<ChatMessageReadVO> readers = chatMessageReadService.getMessageReaders(messageId);
        return Result.success(readers);
    }

    @GetMapping("/message/{messageId}/unread-users")
    @Operation(summary = "查询消息未读用户", description = "查询指定消息的未读用户列表")
    public Result<List<String>> getUnreadUsers(
            @Parameter(description = "消息ID") @PathVariable @NotBlank String messageId) {
        List<String> unreadUsers = chatMessageReadService.getUnreadUsers(messageId);
        return Result.success(unreadUsers);
    }

    @GetMapping("/user/unread-count")
    @Operation(summary = "查询用户未读消息数", description = "查询当前用户的未读消息总数")
    public Result<Integer> getUserUnreadCount(
            @Parameter(description = "群组ID") @RequestParam(required = false) String groupId,
            @Parameter(description = "会话ID") @RequestParam(required = false) String sessionId) {
        String userId = UserContext.getUserId();
        Integer unreadCount = chatMessageReadService.getUserUnreadCount(userId, groupId, sessionId);
        return Result.success(unreadCount);
    }

    @GetMapping("/group/{groupId}/unread-count")
    @Operation(summary = "查询群组未读消息数", description = "查询当前用户在指定群组的未读消息数")
    public Result<Integer> getGroupUnreadCount(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId) {
        String userId = UserContext.getUserId();
        Integer unreadCount = chatMessageReadService.getGroupUnreadCount(groupId, userId);
        return Result.success(unreadCount);
    }

    @GetMapping("/session/{sessionId}/unread-count")
    @Operation(summary = "查询会话未读消息数", description = "查询当前用户在指定会话的未读消息数")
    public Result<Integer> getSessionUnreadCount(
            @Parameter(description = "会话ID") @PathVariable @NotBlank String sessionId) {
        String userId = UserContext.getUserId();
        Integer unreadCount = chatMessageReadService.getSessionUnreadCount(sessionId, userId);
        return Result.success(unreadCount);
    }

    @GetMapping("/message/{messageId}/read-status")
    @Operation(summary = "查询消息已读状态", description = "查询当前用户对指定消息的已读状态")
    public Result<Boolean> getMessageReadStatus(
            @Parameter(description = "消息ID") @PathVariable @NotBlank String messageId) {
        String userId = UserContext.getUserId();
        Boolean isRead = chatMessageReadService.isMessageRead(messageId, userId);
        return Result.success(isRead);
    }

    @GetMapping("/message/{messageId}/read-statistics")
    @Operation(summary = "查询消息已读统计", description = "查询指定消息的已读统计信息")
    public Result<Object> getMessageReadStatistics(
            @Parameter(description = "消息ID") @PathVariable @NotBlank String messageId) {
        Object statistics = chatMessageReadService.getMessageReadStatistics(messageId);
        return Result.success(statistics);
    }

    @GetMapping("/group/{groupId}/read-statistics")
    @Operation(summary = "查询群组已读统计", description = "查询指定群组的消息已读统计信息")
    public Result<Object> getGroupReadStatistics(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "开始时间") @RequestParam(required = false) String startTime,
            @Parameter(description = "结束时间") @RequestParam(required = false) String endTime) {
        Object statistics = chatMessageReadService.getGroupReadStatistics(groupId, startTime, endTime);
        return Result.success(statistics);
    }

    @PostMapping("/update-read-time")
    @Operation(summary = "更新已读时间", description = "更新用户对消息的已读时间")
    public Result<Boolean> updateReadTime(
            @Parameter(description = "消息ID") @RequestParam @NotBlank String messageId) {
        String userId = UserContext.getUserId();
        Boolean result = chatMessageReadService.updateReadTime(messageId, userId);
        return Result.success(result);
    }

    @DeleteMapping("/clear-read-records")
    @Operation(summary = "清理已读记录", description = "清理过期的消息已读记录")
    public Result<Integer> clearReadRecords(
            @Parameter(description = "保留天数") @RequestParam(defaultValue = "30") Integer retentionDays) {
        Integer clearedCount = chatMessageReadService.clearReadRecords(retentionDays);
        return Result.success(clearedCount);
    }

    @GetMapping("/user/read-history")
    @Operation(summary = "查询用户已读历史", description = "查询当前用户的消息已读历史")
    public Result<List<ChatMessageReadVO>> getUserReadHistory(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "页大小") @RequestParam(defaultValue = "20") Integer pageSize) {
        String userId = UserContext.getUserId();
        List<ChatMessageReadVO> readHistory = chatMessageReadService.getUserReadHistory(userId, pageNum, pageSize);
        return Result.success(readHistory);
    }
}
